/*  
cd /projects/data_commons/lbd_code/
qsas lbd_1_raw_fk_flag.sas 5 &
*/

libname llbd "/projects/data_commons/lbd/";
%Let dir_llbd = /projects/data_commons/lbd/;

/* Macro that reads in flag data set */

%macro read_lbd(i_year= );

/* REDACTED
libname raw has been redacted */
libname raw "/REDACTED/";

%put ================================================================================;
%put Year &i_year.;
%put --------------------------------------------------------------------------------;

%Let yver=2016;

%if %eval(&i_year.)<=2009 %then %do;
  %Let naics_keep=lbdid fk_n02_splits fk_n07_splits;
%end;
%else %if %eval(&i_year.)<=2014 %then %do;
  %Let naics_keep=lbdid fk_n02_splits fk_n07_splits;
%end;
%else %do;
  %Let naics_keep=lbdid fk_n07_splits;
%end;


data dti_flag;
  set raw.naics&i_year._flags_c&yver.00 (keep=&naics_keep.);
  year = &i_year.;
run;

%if %sysfunc(exist(dt_flag)) %then %do;
  data dt_flag;
    set dt_flag dti_flag;
  run;
%end;
%else %do;
  data dt_flag;
    set dti_flag;
  run;
%end;

%mend;

/* Macro that executes */

%macro main_read_lbd();
  %Let i_year=1976;
  %do %while(%eval(&i_year.)<=2016);
    %read_lbd(i_year=&i_year.)
    %Let i_year = %eval(&i_year. + 1);
  %end;
%mend;

%main_read_lbd();

proc sort data=dt_flag; by year lbdid;

data llbd.lbd_raw_fk_flag;
  set dt_flag;
run;

/* End of do file */
